package com.bs.visitservice.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.OrderInfo;
import com.bs.visitservice.entity.ReceiverAddress;
import com.bs.visitservice.entity.Seller;
import com.bs.visitservice.service.*;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import com.bs.visitservice.utils.ThreadLocalUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/admin")
public class AdmniOrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private ReceiverAddressService receiverAddressService;
    @Autowired
    private DiscountCouponService discountCouponService;
    @Autowired
    private ProductTypeService productTypeService;
    @RequestMapping(value = "/orderInfoList",method = RequestMethod.POST)
    public Result queryOrderInfo(@RequestBody Map cond){
        OrderInfo param = (OrderInfo) ResultUtil.transObject(cond,OrderInfo.class);
        IPage page = ParamUtils.getPage(cond);
        LambdaQueryWrapper<OrderInfo> wrapper = new LambdaQueryWrapper<>();
        //通过订单编号查询
        if(!StringUtils.isEmpty(param.getOrderNo())){
            wrapper.eq(OrderInfo::getOrderNo,param.getOrderNo());
        }
        Map<String,Object> result = new HashMap<>();
        wrapper.orderByDesc(OrderInfo::getDatatime);
        page = orderInfoService.pageMaps(page,wrapper);
        List<Map<String, Object>> res = page.getRecords();
        res.forEach(map->{
            Long orderNo = (Long)map.get("no");
            //商品列表
            List<Map<String,Object>> proList = productService.queryOrderProduct(orderNo);
            //用户对订单的逻辑删除在商家端依然是已完成状态
            if(4 == (Integer)map.get("orderStatus")){
                map.put("orderStatus",3);
            }
            ResultUtil.transfort(proList, getDictMap(), true);
            map.put("proList", proList);
            //顾客地址
            ReceiverAddress receiveNo = receiverAddressService.getById((Long) map.get("receiveNo"));
            map.put("receiveInfo",receiveNo);
            //订单使用优惠券
            List<Map<String, Object>> discountList = discountCouponService.queryByOrder(orderNo);
            ResultUtil.transfort(discountList,this.getDictMap(),true);
            map.put("disList",discountList);
        });
        ResultUtil.transfort(res, getDictMap(), true);
        result.put("total",page.getTotal());
        result.put("datas",res);
        return Result.success(result);
    }
    /**
     * 获取列和字典值的映射关系
     * @return
     */
    private Map getDictMap(){
        Map<String,String> dictMap = new HashMap<>();
        dictMap.put("orderStatus","orderStatus");
        dictMap.put("type","discountType");
        return dictMap;
    }


}
